package com.hzsd.zengbanyi.biaodan_text.fragment;

import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.hzsd.zengbanyi.biaodan_text.R;
import com.hzsd.zengbanyi.biaodan_text.bean.ApparatusList;
import com.hzsd.zengbanyi.biaodan_text.bean.LabSampleList;
import com.hzsd.zengbanyi.biaodan_text.bean.MethodList;
import com.hzsd.zengbanyi.biaodan_text.bean.ProjectAnalysis;
import com.hzsd.zengbanyi.biaodan_text.bean.SampleList;
import com.hzsd.zengbanyi.biaodan_text.widget.PopupWindowUtil;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import hzst.android.adapter.CommonAdapter;
import hzst.android.util.JsonDataParser;
import hzst.android.util.L;
import hzst.android.util.NetworkDataUtil;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

public class ProjectAnalysisFragment extends Fragment implements
		View.OnClickListener {
	private int index;
	private String item_id;
	private ListView listView;
	private LinearLayout ll_Batch_number;
	private LinearLayout ll_add_value;
	private LinearLayout ll_sample_code_btn;
	private String loginName;
	private int methodIndex;
	private String[] methodList;
	private List<MethodList> methodLists;
	private String name;
	private String new_sample_code_btn;
	private RadioButton radioButton;
	private RadioGroup radioGroup;
	private SampleList sampleList;
	private List<SampleList> sampleLists;
	private String sample_code_btn;
	private String[] sample_code_btnStrings;
	private Spinner sp_apparatus_list;
	private Spinner sp_method_list;
	private String task_id;
	private String task_scene_id;
	private TextView tv_analyse_time;
	private TextView tv_analyse_user_name;
	private TextView tv_entity_name;
	private Button btn_more;
	private String urlPath;
	private View view;

	private String Batch_number;
	private final int MSG_ADD_SAMPLE = 1;
	private final int MSG_DATA = 0;
	private final int MSG_DELETE_SAMPLE = 2;
	private final int MSG_LIST_SAMPLE = 3;
	private CommonAdapter<SampleList> adapter;
	private ArrayAdapter<String> adapterApparatusList = null;
	private ArrayAdapter<String> adapterElement_name = null;
	private ArrayAdapter<String> adapterMethodList = null;
	private ArrayAdapter<String> adapterSample_code_btn = null;
	private String add_value;
	private ProjectAnalysis analysis;
	private LabSampleList labSampleList;
	private int apparatusIndex;
	private String[] apparatusList;
	private List<ApparatusList> apparatusLists;
	private Button btn_add;
	private String element_name;
	private String[] element_nameStrings;
	private int isadd;//是不是加标样，服务器需要
	private List<String> itemList;
	private PopupWindowUtil popupWindowUtil;
	
	private Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case MSG_DATA:
				while (analysis == null)
					;
				if (analysis == null) {
					break;
				}
				btn_add.setVisibility(View.VISIBLE);
				methodLists = analysis.getMethod_list();
				apparatusLists = analysis.getApparatus_list();
				setViewValue();
				initListAdapter();
				break;
			case MSG_ADD_SAMPLE:
				int num = Integer.parseInt((String) msg.obj) ;
				if (num > 0) {
					getSampleList();
				}else{
					Toast.makeText(getActivity(), "添加失败", Toast.LENGTH_LONG).show();
				}
				
				break;
			case MSG_DELETE_SAMPLE:
				int result = Integer.parseInt((String) msg.obj);
				if (result > 0) {
					Toast.makeText(getActivity(), "删除成功", Toast.LENGTH_LONG).show();
					//获取列表数据
					getSampleList();
					//本地删除，不用再获取数据（和上面getSampleList()方法二选一）
//					sampleLists.remove(index);
//					adapter.notifyDataSetChanged();
				}else{
					Toast.makeText(getActivity(), "删除失败", Toast.LENGTH_LONG).show();
				}
				break;
			case MSG_LIST_SAMPLE:
				initListAdapter();
				break;
			}

		}
	};
	
	public View onCreateView(LayoutInflater paramLayoutInflater,
			ViewGroup paramViewGroup, Bundle paramBundle) {
		this.view = LayoutInflater.from(getActivity()).inflate(
				R.layout.fragment_project_analysis, paramViewGroup, false);
		initBaseData();
		initView(this.view);
		initEvents();
		getData();
		return this.view;
	}


	private void initBaseData() {
		SharedPreferences localSharedPreferences = getActivity()
				.getSharedPreferences("user", 0);
		this.urlPath = localSharedPreferences.getString("urlPath", "");
		this.loginName = localSharedPreferences.getString("loginName", "");
		this.name = localSharedPreferences.getString("name", "");
		this.task_id = getArguments().getString("task_id");
		this.task_scene_id = getArguments().getString("task_scene_id");
		this.item_id = getArguments().getString("item_id");
		this.sample_code_btn = getArguments().getString("sample_code_btn");
		
		itemList = new ArrayList<String>();
		itemList.add("清除原始记录");
	}
	
	private void initView(View paramView) {
		this.listView = ((ListView) paramView.findViewById(R.id.lv_listview));
		this.tv_entity_name = ((TextView) paramView
				.findViewById(R.id.tv_entity_name));
		this.tv_analyse_user_name = ((TextView) paramView
				.findViewById(R.id.tv_analyse_user_name));
		this.tv_analyse_time = ((TextView) paramView
				.findViewById(R.id.tv_analyse_time));
		this.sp_method_list = ((Spinner) paramView
				.findViewById(R.id.sp_method_list));
		this.sp_apparatus_list = ((Spinner) paramView
				.findViewById(R.id.sp_apparatus_list));
		this.btn_add = ((Button) paramView.findViewById(R.id.btn_add));
		this.btn_add.setVisibility(View.GONE);
		this.radioGroup = ((RadioGroup) paramView
				.findViewById(R.id.rg_sample_type));
		this.radioButton = ((RadioButton) paramView.findViewById(R.id.rb_pxy));
		this.radioGroup.check(this.radioButton.getId());
		
		btn_more = (Button) paramView.findViewById(R.id.btn_more);
	}

	private void initEvents() {
		this.btn_add.setOnClickListener(this);
		btn_more.setOnClickListener(this);
		this.listView
				.setOnItemLongClickListener(new OnItemLongClickListener() {
					public boolean onItemLongClick(
							AdapterView<?> paramAnonymousAdapterView,
							View paramAnonymousView,
							final int paramAnonymousInt, long paramAnonymousLong) {
						Builder localBuilder = new Builder(
								ProjectAnalysisFragment.this.getActivity());
						localBuilder.setAdapter(new ArrayAdapter(ProjectAnalysisFragment.this.getActivity(), 0, new String[] { "删除" }),
								new OnClickListener() {
									public void onClick(
											DialogInterface paramAnonymous2DialogInterface,
											int paramAnonymous2Int) {
										index = paramAnonymousInt;
										deleteSample();
									}
								});
						localBuilder.create().show();
						return false;
					}
				});
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					final int position, long id) {
				View dialogView = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_save_sample, null, false);
				
				final EditText et_sample_value = (EditText) dialogView.findViewById(R.id.et_sample_value);
				LinearLayout ll_add_value = (LinearLayout) dialogView.findViewById(R.id.ll_add_value);
				final EditText et_add_value = (EditText) dialogView.findViewById(R.id.et_add_value);
				
				if ("加标样".equals(sampleLists.get(position).getSample_type())) {
					ll_add_value.setVisibility(View.VISIBLE);
				}else{
					ll_add_value.setVisibility(View.GONE);
				}
				
				et_sample_value.setText(sampleLists.get(position).getSample_value());
				et_add_value.setText(sampleLists.get(position).getAdd_value());
				
				Builder builder = new Builder(getActivity());
				builder.setTitle(sampleLists.get(position).getSample_type()+sampleLists.get(position).getSample_code());
				builder.setView(dialogView);
				builder.setNegativeButton("取消", new OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						
					}
				});
				builder.setPositiveButton("保存", new OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						updateSample(position,et_sample_value.getText().toString().trim(),et_add_value.getText().toString().trim());
					}
				});
				builder.create().show();
				
			}
		});
		this.radioGroup
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {
					public void onCheckedChanged(
							RadioGroup paramAnonymousRadioGroup,
							int paramAnonymousInt) {
						ProjectAnalysisFragment.this.radioButton = ((RadioButton) ProjectAnalysisFragment.this
								.getActivity().findViewById(
										paramAnonymousRadioGroup
												.getCheckedRadioButtonId()));
					}
				});
		//分析方法Spinner监听事件
		sp_method_list.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				updateSpinnerData();
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				
			}
		});
		//分析仪器Spinner监听事件
		sp_apparatus_list.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				updateSpinnerData();
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				
			}
		});
	}
	
	private void getData() {
		new Thread() {
			public void run() {
				if (analysis == null) {
					analysis = new ProjectAnalysis();
				}
				String str = "http://"
						+ ProjectAnalysisFragment.this.urlPath
						+ "/ServiceInterface/OriginalRecord/ProjectAnalysis.ashx?method=detail&task_id="
						+ ProjectAnalysisFragment.this.task_id
						+ "&task_scene_id="
						+ ProjectAnalysisFragment.this.task_scene_id
						+ "&item_id=" + ProjectAnalysisFragment.this.item_id
						+ "&UserCode=" + ProjectAnalysisFragment.this.loginName
						+ "&TrueName=" + ProjectAnalysisFragment.this.name;
				analysis = ((ProjectAnalysis) JsonDataParser
						.getBean(NetworkDataUtil.getJsonData(str),
								ProjectAnalysis.class));
				Message msg = Message.obtain();
				msg.what = MSG_DATA;
				handler.sendMessage(msg);
			}
		}.start();
	}
	/**
	 * 获取样品列表数据
	 */
	public void getSampleList() {
		new Thread(){
			public void run() {
				sampleLists.clear();
				if (labSampleList == null) {
					labSampleList = new LabSampleList();
				}
				String url = "http://"+urlPath+"/ServiceInterface/OriginalRecord/ProjectSample.ashx?method=list&hzstId="+analysis.getHzstId();
				labSampleList = JsonDataParser.getBean(NetworkDataUtil.getJsonData(url), LabSampleList.class);
				sampleLists.addAll(labSampleList.getSample_list());
				Message msg = Message.obtain();
				msg.what = MSG_LIST_SAMPLE;
				handler.sendMessage(msg);
			};
		}.start();
	}
	

	private void setViewValue() {
		tv_entity_name.setText(analysis.getEntity_name());
		tv_analyse_user_name.setText(analysis.getAnalyse_user_name());
		tv_analyse_time.setText(analysis.getAnalyse_time());
		methodList = new String[1 + methodLists.size()];
		methodList[0] = "";
		for (int i = 0; i < methodLists.size(); i++) {
			methodList[(i + 1)] = ((MethodList) this.methodLists.get(i))
					.getMethod_name();
			if (this.analysis.getAnalyse_method_code().equals(
					((MethodList) this.methodLists.get(i)).getMethod_id())) {
				this.methodIndex = (i + 1);
			}

		}
		if (adapterMethodList == null) {
			adapterMethodList = new ArrayAdapter(getActivity(),
					android.R.layout.simple_spinner_item, this.methodList);
			adapterMethodList
					.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			sp_method_list.setAdapter(this.adapterMethodList);
		}
		sp_method_list.setSelection(this.methodIndex);

		apparatusList = new String[1 + this.apparatusLists.size()];
		apparatusList[0] = "";
		for (int j = 0; j < apparatusLists.size(); j++) {
			apparatusList[(j + 1)] = ((ApparatusList) this.apparatusLists
					.get(j)).getApparatus_type_name();
			if (analysis.getMethod_Code().equals(
					((ApparatusList) this.apparatusLists.get(j))
							.getApparatus_code())) {
				apparatusIndex = (j + 1);
			}
		}
		if (adapterApparatusList == null) {
			adapterApparatusList = new ArrayAdapter(getActivity(),
					android.R.layout.simple_spinner_item, this.apparatusList);
			adapterApparatusList
					.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			sp_apparatus_list.setAdapter(this.adapterApparatusList);
		}
		sp_apparatus_list.setSelection(this.apparatusIndex);
	}

	/**
	 * 向服务器请求添加样品
	 */
	public void addSample() {
		new Thread() {
			public void run() {
				String url = "http://"+urlPath+"/ServiceInterface/OriginalRecord/ProjectSample.ashx?method=add"+
						"&task_id="+task_id+
						"&task_scene_id="+task_scene_id+
						"&item_id"+item_id+
						"&hzstId="+analysis.getHzstId()+
						"&sample_code="+sampleList.getSample_code()+
						"&item_name="+URLEncoder.encode(sampleList.getItem_name())+
						"&reagent_num="+URLEncoder.encode(Batch_number)+
						"&sampletype="+URLEncoder.encode(sampleList.getSample_type())+
						"&isadd="+isadd+
						"&add_value="+URLEncoder.encode(sampleList.getAdd_value())+
						"&sample_code_pre="+URLEncoder.encode(sample_code_btn);
				String json = NetworkDataUtil.getJsonData(url);
//				System.out.println("添加样品："+url);
//				System.out.println("添加样品json返回值："+json);
				Message msg = Message.obtain();
				msg.what = MSG_ADD_SAMPLE;
				msg.obj = json;
				handler.sendMessage(msg);
			}
		}.start();
	}

	/**
	 * 向服务器请求删除样品
	 */
	public void deleteSample() {
		new Thread() {
			public void run() {
				String url = "http://"+urlPath+"/ServiceInterface/OriginalRecord/ProjectSample.ashx?method=delete&hzst_itemId="+sampleLists.get(index).getHzst_itemId();
				String json = NetworkDataUtil.getJsonData(url);
				L.showLogInfo("删除样品：", url);
				L.showLogInfo("删除样品json返回值：", json);
//				System.out.println("删除样品："+ url);
//				System.out.println("删除样品json返回值："+ json);
				Message msg = Message.obtain();
				msg.what = MSG_DELETE_SAMPLE;
				msg.obj = json;
				handler.sendMessage(msg);
			}
		}.start();
	}

	public void getSpinnerChangeText(final Spinner paramSpinner,
			final TextView paramTextView) {
		paramSpinner
				.setOnItemSelectedListener(new OnItemSelectedListener() {
					public void onItemSelected(
							AdapterView<?> paramAnonymousAdapterView,
							View paramAnonymousView, int paramAnonymousInt,
							long paramAnonymousLong) {
						paramTextView.setText((CharSequence) paramSpinner
								.getSelectedItem());
					}

					public void onNothingSelected(
							AdapterView<?> paramAnonymousAdapterView) {
					}
				});
	}

	public void isHidenView() {
		String str = this.radioButton.getText().toString();
		switch (str) {
		case "加标样":
			this.ll_Batch_number.setVisibility(View.GONE);
			this.ll_sample_code_btn.setVisibility(View.VISIBLE);
			this.ll_add_value.setVisibility(View.VISIBLE);
			break;
		case "平行样":
			this.ll_Batch_number.setVisibility(View.GONE);
			this.ll_sample_code_btn.setVisibility(View.VISIBLE);
			this.ll_add_value.setVisibility(View.GONE);
			break;
		case "空白样":
			this.ll_Batch_number.setVisibility(View.GONE);
			this.ll_sample_code_btn.setVisibility(View.GONE);
			this.ll_add_value.setVisibility(View.GONE);
			break;
		case "质控样":
			this.ll_Batch_number.setVisibility(View.VISIBLE);
			this.ll_sample_code_btn.setVisibility(View.GONE);
			this.ll_add_value.setVisibility(View.GONE);
			break;
		}

	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_add:
			View localView = LayoutInflater.from(getActivity()).inflate(
					R.layout.dialog_add_sample, null, false);
			this.ll_Batch_number = ((LinearLayout) localView
					.findViewById(R.id.ll_Batch_number));
			this.ll_sample_code_btn = ((LinearLayout) localView
					.findViewById(R.id.ll_sample_code_btn));
			this.ll_add_value = ((LinearLayout) localView
					.findViewById(R.id.ll_add_value));
			final EditText et_Batch_number = (EditText) localView
					.findViewById(R.id.et_Batch_number);
			final EditText et_new_sample_code_btn = (EditText) localView
					.findViewById(R.id.et_new_sample_code_btn);
			final EditText et_add_value = (EditText) localView
					.findViewById(R.id.et_add_value);
			final Spinner sp_element_name = (Spinner) localView
					.findViewById(R.id.sp_element_name);
			Spinner sp_sample_code_btn = (Spinner) localView
					.findViewById(R.id.sp_sample_code_btn);
			isHidenView();
			if (this.element_nameStrings == null) {
				this.element_nameStrings = new String[2];
				this.element_nameStrings[0] = "";
				this.element_nameStrings[1] = this.analysis.getItem_name();
			}
			if (this.adapterElement_name == null) {
				this.adapterElement_name = new ArrayAdapter(getActivity(),
						android.R.layout.simple_spinner_item,
						this.element_nameStrings);
				this.adapterElement_name
						.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			}
			sp_element_name.setAdapter(this.adapterElement_name);

			if (this.sample_code_btnStrings == null) {
				this.sample_code_btnStrings = new String[2];
				this.sample_code_btnStrings[0] = "";
				this.sample_code_btnStrings[1] = this.sample_code_btn;
			}
			if (this.adapterSample_code_btn == null) {
				this.adapterSample_code_btn = new ArrayAdapter(getActivity(),
						android.R.layout.simple_spinner_item,
						this.sample_code_btnStrings);
				this.adapterSample_code_btn
						.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			}
			sp_sample_code_btn.setAdapter(this.adapterSample_code_btn);
			getSpinnerChangeText(sp_sample_code_btn, et_new_sample_code_btn);
			Builder localBuilder = new Builder(
					getActivity());
			localBuilder.setTitle(this.radioButton.getText().toString());
			localBuilder.setView(localView);
			localBuilder.setNegativeButton("取消",
					new OnClickListener() {
						public void onClick(
								DialogInterface paramAnonymousDialogInterface,
								int paramAnonymousInt) {
						}
					});
			localBuilder.setPositiveButton("添加",
					new OnClickListener() {
						public void onClick(
								DialogInterface paramAnonymousDialogInterface,
								int paramAnonymousInt) {
							new_sample_code_btn = et_new_sample_code_btn
									.getText().toString();
							Batch_number = et_Batch_number.getText().toString();
							add_value = et_add_value.getText().toString();
							element_name = sp_element_name.getSelectedItem()
									.toString();
							if ("加标样".equals(radioButton.getText().toString())) {
								isadd = 1;
							}else{
								isadd = 0;
							}
							sampleList = new SampleList();
							sampleList.setAdd_value(add_value);
							sampleList.setSample_code(new_sample_code_btn);
							sampleList.setSample_type(radioButton.getText()
									.toString());
							sampleList.setItem_name(element_name);
							addSample();
						}
					});
			localBuilder.create().show();
			break;
		case R.id.btn_more:
			if (popupWindowUtil==null) {
				popupWindowUtil = new PopupWindowUtil(getActivity(), itemList);
			}
			popupWindowUtil.setItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {
					popupWindowUtil.dismiss();
					Toast.makeText(getActivity(),itemList.get(position), Toast.LENGTH_LONG).show();
					switch (itemList.get(position)) {
					case "清除原始记录":
						
						break;

					default:
						break;
					}
					
				}
			});
			popupWindowUtil.show(v, 4);
			break;
		}

	}
	/**
	 * 初始化列表的适配器
	 */
	public void initListAdapter() {
		if (adapter == null) {
			sampleLists = new ArrayList<SampleList>();
			sampleLists = analysis.getSample_list();
			adapter = new CommonAdapter<SampleList>(getActivity(), sampleLists, R.layout.item_sample_list) {
				public void convert(ViewHolder helper, SampleList item, int position) {
					helper.setText(R.id.tv_item_name,
							item.getItem_name());
					helper.setText(R.id.tv_sample_type,
							item.getSample_type());
					helper.setText(R.id.tv_sample_code_btn,
							item.getSample_code());
					helper.setText(R.id.tv_sample_value,
							item.getSample_value());
					helper.setText(R.id.tv_add_value,
							item.getAdd_value());
					if (position % 2 != 0) {
						helper.setBackground(R.id.ll_itembg,R.color.blue_shallow_1);
					} else {
						helper.setBackground(R.id.ll_itembg,R.color.blue_shallow_2);
					}
				};
			};
			listView.setAdapter(adapter);
		} else {
			adapter.notifyDataSetChanged();
		}
	}
	
	/**
	 * 清除原始记录
	 */
	public void clearOriginalRecord() {
		new Thread(){
			public void run() {
				
			};
		}.start();
	}
	
	/**
	 * 向服务器更新分析方法、分析仪器选定项
	 */
	public void updateSpinnerData() {
		
		String url = "http://"+urlPath+"/ServiceInterface/OriginalRecord/ProjectAnalysis.ashx?method=updata&hzstId="+analysis.getHzstId()+"&json="+URLEncoder.encode(getSpinnerJson());
		Observable.just(url)
		.map(new Func1<String,String>() {

			@Override
			public String call(String url) {
				return NetworkDataUtil.getJsonData(url);
			}
		}).subscribeOn(Schedulers.io())
		.observeOn(AndroidSchedulers.mainThread())
		.subscribe(new Action1<String>() {

			@Override
			public void call(String arg0) {
				// TODO Auto-generated method stub
				
			}
		});
	}
	/**
	 * 获取Spinner的选中值并转化json
	 * @return
	 */
	public String getSpinnerJson() {
		Map<String, String> map = new HashMap<String, String>();
		if (sp_apparatus_list.getSelectedItemPosition() > 0) {
			map.put("method_Code", apparatusLists.get(sp_apparatus_list.getSelectedItemPosition()-1).getApparatus_code());
			map.put("method_name", apparatusLists.get(sp_apparatus_list.getSelectedItemPosition()-1).getApparatus_name());
		}else{
			map.put("method_Code", "");
			map.put("method_name", "");
		}
		if (sp_method_list.getSelectedItemPosition() > 0) {
			map.put("analyse_method_code", methodLists.get(sp_method_list.getSelectedItemPosition()-1).getMethod_id());
			map.put("analyse_method_name", methodLists.get(sp_method_list.getSelectedItemPosition()-1).getMethod_name());
		}else{
			map.put("analyse_method_code", "");
			map.put("analyse_method_name", "");
		}
		Gson gson = new Gson();
		return gson.toJson(map);
		
	}
	/**
	 * 修改/保存样品
	 */
	public void updateSample(int position,String sample_value,String add_value) {
		String url = "http://"+urlPath+"/ServiceInterface/OriginalRecord/ProjectSample.ashx?method=updata&hzst_itemId="+sampleLists.get(position).getHzst_itemId()+"&sample_value="+sample_value+"&add_value="+add_value;
		System.out.println("修改/保存样品:"+url);
		Observable.just(url)
		.map(new Func1<String, String>() {

			@Override
			public String call(String url) {
				return NetworkDataUtil.getJsonData(url);
			}
		})
		.subscribeOn(Schedulers.io())
		.observeOn(AndroidSchedulers.mainThread())
		.subscribe(new Action1<String>() {

			@Override
			public void call(String arg0) {
				getSampleList();//重新获取数据，刷新页面
			}
		});
	}

	
}